package com.inspur.gs.sbssdl.sbwhgl.gyjgl.gyjgl.front.gyjlysqxz2.voactions;
import com.inspur.edp.bef.api.lcp.ILcpFactory;
import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bef.api.services.IBefSessionManager;
import com.inspur.edp.bff.api.manager.context.QueryContext;
import com.inspur.edp.bff.spi.action.query.AbstractQueryAction;
import com.inspur.edp.cef.entity.condition.*;
import com.inspur.edp.cef.entity.entity.IEntityData;
import com.inspur.edp.cef.entity.entity.IEntityDataCollection;
import com.inspur.edp.cef.entity.entity.IValuesContainer;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

public class queryDataVOAction extends AbstractQueryAction {

	public queryDataVOAction(QueryContext context){
		super(context);
	}
	@Override
	public void execute() {
		SpringBeanUtils.getBean(IBefSessionManager.class).createSession();
		try {
			//点检配置变更
			ILcpFactory iLcpFactory = SpringBeanUtils.getBean(ILcpFactory.class);
			String tzid = "46911fe3-0d25-4e16-9e9e-c93393525ced";
			String sqid = "07405736-2927-4204-bca3-f099630cac07";
			IStandardLcp tzlcp = iLcpFactory.createLcpByBEId(tzid);
			IStandardLcp sqlcp = iLcpFactory.createLcpByBEId(sqid);

			EntityFilter filter= new EntityFilter();
			ArrayList<FilterCondition> list = new ArrayList<>();



			EntityFilter filterParams = getQueryContext().getFilter();
			String gyjmc = filterParams.getFilterConditions().stream()
					.filter(s -> s.getFilterField().equals("gyjmc"))
					.map(s -> s.getValue())
					.findFirst()
					.orElse(null);
			if (gyjmc!=null){
				FilterCondition filterCondition1 = new FilterCondition(0, "gyjmc", ExpressCompareType.Like, gyjmc, 0, ExpressRelationType.And, ExpressValueType.Value);
				list.add(filterCondition1);
			}

			String lxmc = filterParams.getFilterConditions().stream()
					.filter(s -> s.getFilterField().equals("lxmc"))
					.map(s -> s.getValue())
					.findFirst()
					.orElse(null);

			if (lxmc!=null){
				FilterCondition filterCondition2 = new FilterCondition(0, "lx_lxmc", ExpressCompareType.Equal, lxmc, 0, ExpressRelationType.And, ExpressValueType.Value);
				list.add(filterCondition2);
			}

			FilterCondition filterCondition3 = new FilterCondition(0, "zt", ExpressCompareType.Equal, "0", 0, ExpressRelationType.Empty, ExpressValueType.Value);
			list.add(filterCondition3);
			filter.addFilterConditions(list);
			List<IEntityData> tzDataList = tzlcp.query(filter);

			EntityFilter sqfilter= new EntityFilter();
			ArrayList<FilterCondition> sqlist = new ArrayList<>();
			FilterCondition sqfilterCondition = new FilterCondition(0, "BillStatus", ExpressCompareType.Equal, "0", 0, ExpressRelationType.Or, ExpressValueType.Value);
			FilterCondition sqfilterCondition1 = new FilterCondition(0, "BillStatus", ExpressCompareType.Equal, "1", 0, ExpressRelationType.Empty, ExpressValueType.Value);
			sqlist.add(sqfilterCondition);
			sqlist.add(sqfilterCondition1);
			sqfilter.addFilterConditions(sqlist);
			List<IEntityData> sqDataList = sqlcp.query(sqfilter);

			List<String> sqgyjids = new ArrayList<>();
			for (IEntityData sqitem : sqDataList) {
				String id = sqitem.getID();
				IEntityDataCollection lymxList = sqlcp.retrieve(id).getData().getChilds().get("lymx");
				for (IEntityData lymx : lymxList) {
					String mxid = (String) ((IValuesContainer)lymx.getValue("tzid")).getValue("tzid");
					sqgyjids.add(mxid);
				}
			}

			List<IEntityData> filteredList = tzDataList.stream()
					.filter(s -> !sqgyjids.contains(s.getID()))
					.collect(Collectors.toList());

			ArrayList<IEntityData> entityData=new ArrayList<>();
			for (IEntityData tzitem : filteredList) {
				IEntityData voEntityData =getContext().createData();
				String uuid = UUID.randomUUID().toString();
				voEntityData.setValue("ID",uuid);
				voEntityData.setValue("tzid",tzitem.getValue("ID"));
				voEntityData.setValue("debh",tzitem.getValue("debh"));
				voEntityData.setValue("gyjmc",tzitem.getValue("gyjmc"));
				voEntityData.setValue("lxmc",((IValuesContainer)tzitem.getValue("lx")).getValue("lx_lxmc"));
				voEntityData.setValue("ggxh",tzitem.getValue("ggxh"));
				voEntityData.setValue("dw",tzitem.getValue("dw"));
				voEntityData.setValue("dnhsdj",tzitem.getValue("dnhsdj"));
				voEntityData.setValue("sccj",tzitem.getValue("sccj"));
				voEntityData.setValue("gzrq",tzitem.getValue("gzrq"));
				voEntityData.setValue("cfwz",tzitem.getValue("cfwz"));
				voEntityData.setValue("synx",tzitem.getValue("synx"));
				voEntityData.setValue("lx",((IValuesContainer)tzitem.getValue("lx")).getValue("lx"));
				entityData.add(voEntityData);
			}
			HashMap<String,ArrayList<IEntityData>> queryResult = new HashMap<String,ArrayList<IEntityData>>();
			queryResult.put("queryresult",entityData);
			setResult(queryResult);

		} finally {
			SpringBeanUtils.getBean(IBefSessionManager.class).closeCurrentSession();
		}
	}
}
